#include "setdlg.h"
#include "ui_setdlg.h"
#include <QDebug>

SetDlg::SetDlg(QWidget *parent,CSql* sql) :
    QDialog(parent),
    ui(new Ui::SetDlg),
    m_sql(sql)
{
    ui->setupUi(this);

    this->setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint);

    this->setWindowTitle(_("SysSetting"));

    std::string str,IP1 = "",IP2,IP3,Port1,Port2,Port3,Port4,Port5,chkstart;
    //str = "insert into SetUp(para,value) values('BackPort','54601')";
    //m_sql->exeSql(str);

    str = "select * from SetUp;";
    m_sql->exeSql("select * from SetUp");
    m_sql->GetValue("LocalIP",IP1);
    ui->LElocalIP->setText(QString(IP1.c_str()));

    m_sql->GetValue("RemoteIP",IP2);
    ui->LERemoteIP->setText(QString(IP2.c_str()));

    m_sql->GetValue("LocalPort",Port1);
    ui->LElocalPort1->setText(QString(Port1.c_str()));

    m_sql->GetValue("LocalPort1",Port2);
    ui->LElocalPort2->setText(QString(Port2.c_str()));

    m_sql->GetValue("RemotePort",Port3);
    ui->LERemotePort->setText(QString(Port3.c_str()));

    m_sql->GetValue("BackPort",Port4);
    ui->LEBackPort->setText(QString(Port4.c_str()));

    m_sql->GetValue("MIS-IP",IP3);
    ui->LEMISIP->setText(QString(IP3.c_str()));

    m_sql->GetValue("MIS-Port",Port5);
    ui->LEMISPort->setText(QString(Port5.c_str()));

    m_sql->GetValue("MIS-Start",chkstart);
    ui->ckbMIS->setCheckState(chkstart=="1" ? Qt::Checked:Qt::Unchecked);


    connect(ui->pBOK,SIGNAL(clicked()),this,SLOT(OnOK()));
    connect(ui->pBCancel,SIGNAL(clicked()),this,SLOT(reject()));
}

void SetDlg::OnOK()
{
    QString qsql,chkstate="0";
    if(ui->ckbMIS->checkState()==Qt::Checked)
        chkstate="1";

    qsql = QString("update SetUp set value='%1'  where para = 'LocalIP';"
                   "update SetUp set value='%2'  where para = 'RemoteIP';"
                   "update SetUp set value='%3' where para = 'LocalPort';"
                   "update SetUp set value='%4' where para = 'LocalPort1';"
                   "update SetUp set value='%5' where para = 'RemotePort';"
                   "update SetUp set value='%6' where para = 'BackPort';"
                   "update SetUp set value='%7' where para = 'MIS-IP';"
                   "update SetUp set value='%8' where para = 'MIS-Port';"
                   "update SetUp set value='%9' where para = 'MIS-Start';"
                   ).arg(ui->LElocalIP->text())
                    .arg(ui->LERemoteIP->text())
                    .arg(ui->LElocalPort1->text())
                    .arg(ui->LElocalPort2->text())
                    .arg(ui->LERemotePort->text())
                    .arg(ui->LEBackPort->text(),ui->LEMISIP->text(),ui->LEMISPort->text(),chkstate);
    m_sql->exeSql(qsql.toStdString());

    //m_sql->SetValue("LocalIP",ui->LElocalIP->text().toStdString());
    //m_sql->SetValue("RemoteIP",ui->LERemoteIP->text().toStdString());
    //m_sql->SetValue("LocalPort",ui->LElocalPort1->text().toStdString());
    //m_sql->SetValue("LocalPort1",ui->LElocalPort2->text().toStdString());
    //m_sql->SetValue("RemotePort",ui->LERemotePort->text().toStdString());

    //qDebug()<<qsql;
    QDialog::accept();

}
SetDlg::~SetDlg()
{
    delete ui;
}
